#!/usr/bin/env python

""" This is a little demo of the widget module.

    It shows how to create a form that can self-replicate as a result
    of the user clicking a button, and close itself as a result of the
    user clicking another button. When the last form is closed, the
    program ends.
    
    The main Pygame window is resizable, and each new form will
    appear at a random place inside the Pygame window, which means the
    bigger your (actual) desktop is, the more space you have to play
    with.
    
    On my system running at 2GHz, there is no noticable slowdown until
    there are 8-9 forms, and only when they are stacked.
"""

# This lets the script run from inside the example dir of the main repo trunk,
# even if Scrollback hasn't been installed the user's system.
import sys
sys.path.append('..')

# This is for randomizing window positions.
import random

# These have to be imported separately because sbak doesn't import widget.
import sbak
import sbak.widget

from sbak.locals import *

class MyApp(sbak.app.App):
    def __init__(self):
        
        # Initialize ScrollBack and the widget subsystem.
        # Again, sbak.widget is separated from the rest of ScrollBack, so it
        # must be initialized explicitly.
        sbak.init()
        sbak.widget.init()
        
        # Initialize this app.
        sbak.app.App.__init__(self)
        
        # This counter counts the number of forms on the screen.
        # When it reaches 0, the program terminates
        self.form_count = 0
        
        # First, create a desktop. This initializes the screen.
        # (We'll use the default region provided by ScrollBack.)
        self.desktop = sbak.widget.Desktop(
            # Let's make it so the user can resize the desktop window.
            resizable = True
        )
        
        # Append the desktop to the app so it can post and receive events.
        self.append(self.desktop)
        
        # Put an initial form on the desktop.
        self.make_form()

    
    def make_form(self, e=None):
        """ Make a new form at a random place on the screen.
        
            This takes two arguments because we will be using it as an
            event handler, and the second argument is optional because
            we will also be using it as a stand-alone function.
        """
        
        # Increment the window count.
        self.form_count += 1
        
        # Create the form.
        frm = DuplicationForm(
            # Use a static size with a randomly-generated position.
            region = (
                random.randint(0, self.desktop.region.width-200),
                random.randint(0, self.desktop.region.height-150),
                200, 150,
            ),
            
            # Title the form with its number.
            text = "Form #%i" % self.form_count
        )
        frm.id = str(self.form_count)
        #frm.margin = 20
        
        # Create a button to make another form.
        btn1 = sbak.widget.Button(
            region = (0,0,100,50),
            text = "Duplicate!",
            action = self.make_form
        )
        
        # Make an enclosed handler function for the Close button (defined
        # below). We define it here so that it will have access to the form
        # and the app as `self`.
        def handle_clicked(e):
            # Remove the form from the desktop.
            frm.kill()
            
            # Decrement the form count.
            self.form_count -= 1
            
            # If there are no more forms, then finish the app.
            if self.form_count <= 0:
                self.finish()
        
        # Create a button to close the form.
        btn2 = sbak.widget.Button(
            region = (100,0,100,50),
            text = "Close",
            action = handle_clicked
        )
        
        # Let's put the buttons in a container--just because we can!
        cont = sbak.widget.Container(
            region = (0,100,200,50),
            widgets = (btn1,btn2)
        )
        
        # Attach the container to the form.
        frm.append(cont)
        
        # Attach the form to the desktop.
        self.desktop.append(frm)

class DuplicationForm(sbak.widget.Form):
    pass

# If the script is being called (rather than imported)...
if __name__ == '__main__':
     # Create an instance of our app.
    myapp = MyApp()
    
    # Start the main event loop.
    myapp.start()